package com.tre.jdevtemplateboot.common.util;

import com.tre.jdevtemplateboot.common.dto.MailBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailServiceUtils {

    @Autowired(required=false)
    private JavaMailSender javaMailSender;

    public void sendSimpleMail(MailBean mailBean) throws MailException {

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(mailBean.getSender());
        simpleMailMessage.setTo(mailBean.getRecipient());
        simpleMailMessage.setSubject(mailBean.getSubject());
        simpleMailMessage.setText(mailBean.getContent());

        javaMailSender.send(simpleMailMessage);
    }

    public void sendHTMLMail(MailBean mailBean, StringBuilder sb) throws MessagingException {

        MimeMessage mimeMailMessage = null;
        mimeMailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
        mimeMessageHelper.setFrom(mailBean.getSender());
        mimeMessageHelper.setTo(mailBean.getRecipient());
        mimeMessageHelper.setSubject(mailBean.getSubject());
        mimeMessageHelper.setText(sb.toString(), true);

        javaMailSender.send(mimeMailMessage);
    }

    public void sendHTMLMultiRecipientMail(MailBean mailBean, StringBuilder sb) throws MessagingException {

        MimeMessage mimeMailMessage = null;
        mimeMailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
        mimeMessageHelper.setFrom(mailBean.getSender());
        mimeMessageHelper.setTo(mailBean.getArrRecipient());
        mimeMessageHelper.setSubject(mailBean.getSubject());
        mimeMessageHelper.setText(sb.toString(), true);

        javaMailSender.send(mimeMailMessage);
    }

    public void sendAttachmentMail(MailBean mailBean, File file) throws MessagingException {

        MimeMessage mimeMailMessage = null;
        mimeMailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
        mimeMessageHelper.setFrom(mailBean.getSender());
        mimeMessageHelper.setTo(mailBean.getArrRecipient());
        mimeMessageHelper.setSubject(mailBean.getSubject());
        mimeMessageHelper.setText(mailBean.getContent());
        FileSystemResource fsr = new FileSystemResource(file);
        mimeMessageHelper.addAttachment(file.getName(), fsr);

        javaMailSender.send(mimeMailMessage);
    }

    public void sendHTMLMultiRecipientAttachmentMail(MailBean mailBean, StringBuilder sb, File file)
            throws MessagingException {

        MimeMessage mimeMailMessage = null;

        mimeMailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
        mimeMessageHelper.setFrom(mailBean.getSender());
        mimeMessageHelper.setTo(mailBean.getArrRecipient());
        mimeMessageHelper.setSubject(mailBean.getSubject());
        mimeMessageHelper.setText(sb.toString(), true);
        FileSystemResource fs = new FileSystemResource(file);
        mimeMessageHelper.addAttachment(file.getName(), fs);

        javaMailSender.send(mimeMailMessage);
    }

}
